﻿
using UnityEngine;
using BridgeShips.AI;
using Framework.Primitive;
using Framework.FSM;
using BridgeShips.FSM;


namespace BridgeShips
{
    public class EntityLaserLightControl : IControl
    {

        private uint uID;

        AIShip m_aiShip = null;
        GameObject m_ShipCylinderLight = null;
        // private bool m_postInitialize = false;

        public enum ELaserColor
        {
            LASER_HIDE,     //NULL
            LASER_RED,      //red matrial
            LASER_GREEN     //green matrial
        }
        ELaserColor m_EColor = ELaserColor.LASER_HIDE;
        public void Init(EntityBase entity)
        {
            VehicleEntity shipeEntity = entity as VehicleEntity;
            if (shipeEntity != null)
            {
                uID = shipeEntity.GetID();
            }
        }

        public void InitDown()
        {

        }

        public void PostInit()
        {
            var shipeEntity = GameEntry.Instance.EntityManager.Get(uID);
            if (shipeEntity != null)
            {
                m_aiShip = shipeEntity.GetAI();
                if (m_aiShip != null && m_aiShip.Settings != null)
                {
                    float meshLen = m_aiShip.Settings.BridgeShipLenght;
                    if (m_ShipCylinderLight == null)
                    {
                        var trans = m_aiShip.transform.Find("light_belt");
                        m_ShipCylinderLight = trans.gameObject;
                    }
                }
            }
        }

        public void ChangeLaserMaterial(ELaserColor eLaser)
        {
            if (m_EColor == eLaser || m_ShipCylinderLight == null)
                return;
            m_EColor = eLaser;
            //var mats = m_ShipCylinderLight.GetComponent<MeshRenderer>().sharedMaterials;
            //for (int i = 0; i < mats.Length; i++)
            //{
            //    mats[i].SetColor("MainColor", Color.green);
            //}
              int length = m_ShipCylinderLight.GetComponent<MeshRenderer>().materials.Length;
            switch (eLaser)
            {
            
                case ELaserColor.LASER_RED:
                case ELaserColor.LASER_HIDE:
                    {
                        if (m_ShipCylinderLight.GetComponent<MeshRenderer>().materials.Length >= 2)
                            m_ShipCylinderLight.GetComponent<MeshRenderer>().materials[1].color = Color.red;
                    }
                    break;
                case ELaserColor.LASER_GREEN:
                    {
                        if (m_ShipCylinderLight.GetComponent<MeshRenderer>().materials.Length >= 2)
                            m_ShipCylinderLight.GetComponent<MeshRenderer>().materials[1].color = Color.green;
                    }
                    break;
            }


        }

        public void Release()
        {
            //Debug.Log( "MoveManagerTest Release" );
        }

        public void Update(float frameTime, float realTime)
        {

            if (m_aiShip == null || m_aiShip.Settings == null || m_ShipCylinderLight == null)
                return;
            float meshLen = m_aiShip.Settings.BridgeShipLenght * -0.5f - 1.0f;
            //m_ShipCylinderLight.GetComponent<Transform>().SetPositionAndRotation(
            //    m_aiShip.transform.TransformPoint( new Vector3( meshLen, 3.0f, 0.0f ) ), m_aiShip.transform.rotation * Quaternion.Euler( Vector3.up * 0.0f ) );

            var e = GameEntry.Instance.EntityManager.Get(uID);
            var briShip = e.Get().GetComponentInParent<AIEventHandler>();
            if (briShip == null)
                return;
            uint shipID = m_aiShip.Settings.Detection.GetEnity().GetID();
            IFsmState fState = null;
            bool beFar = TransferOfAxes.Instance.linkedEachOtherFarfromSetPoint(e.Get().transform.position);
            if (beFar || !GameEntry.Instance.FsmManager.GetOpenAuxillaryConnection())
            {
                fState = GameEntry.Instance.VirtualRManager.GetFsmHalfWayStateByShipID(shipID);
                bool bCondition = m_aiShip.Settings.Movement.MovementState == ET.AIMovementState.AIDriving ||
                   m_aiShip.Settings.Movement.MovementState == ET.AIMovementState.Idle;
                if (bCondition && (fState.ToString() == "BridgeShips.FSM.VHalfWayLinkingState" ||
                    fState.ToString() == "BridgeShips.FSM.VHalfWayPosCorrectState"))
                {
                    UpdateLader(ELaserColor.LASER_RED);
                }
                else if (fState.ToString() == "BridgeShips.FSM.VHalfWayIdleState")
                {
                    UpdateLader(ELaserColor.LASER_GREEN);
                }
                else if (fState.ToString() == "BridgeShips.FSM.VHalfWayProbeState")
                {
                    UpdateLader(ELaserColor.LASER_HIDE);
                }

            }
            else if (GameEntry.Instance.FsmManager.GetOpenAuxillaryConnection())
            {
                fState = GameEntry.Instance.VirtualRManager.GetFsmStateByShipID(shipID);
                if (fState == null)
                    return;
                if (fState.ToString() == "BridgeShips.FSM.VLinkingState" || fState.ToString() == "BridgeShips.FSM.VPosCorrectState")
                {
                    UpdateLader(ELaserColor.LASER_RED);
                }
                else if (fState.ToString() == "BridgeShips.FSM.VIdleState")
                {
                    UpdateLader(ELaserColor.LASER_GREEN);
                }
                else if (fState.ToString() == "BridgeShips.FSM.VAddressCorrectState")
                {
                    UpdateLader(ELaserColor.LASER_HIDE);
                }
            }
        }

        public MeshRenderer GetCylinderLightMeshRenderer()
        {
            return m_ShipCylinderLight.GetComponent<MeshRenderer>();
        }


        private void UpdateLader(ELaserColor eLaser)
        {
            if (eLaser == ELaserColor.LASER_RED)
            {
                if (!GetCylinderLightMeshRenderer().enabled)
                    GetCylinderLightMeshRenderer().enabled = true;
                ChangeLaserMaterial(ELaserColor.LASER_RED);
            }
            else if (eLaser == ELaserColor.LASER_GREEN)
            {
                if (!GetCylinderLightMeshRenderer().enabled)
                    GetCylinderLightMeshRenderer().enabled = true;
                ChangeLaserMaterial(ELaserColor.LASER_GREEN);
            }
            else
            {
                if (GetCylinderLightMeshRenderer().enabled)
                    GetCylinderLightMeshRenderer().enabled = false;
            }
        }
    }
}
